#pragma once
#include <typescommon.h>
#include "rbtree_augmented.h"
enum rbtreeKeyTypeEnum
{
    rbtreeKeyType_32 = 0,
    rbtreeKeyType_64,
    rbtreeKeyType_Str,
    rbtreeKeyType_Mem,
    rbtreeKeyType_Max,
};
#define RB_TREE_KEY_STR_MAX_SIZE 16
// #pragma pack(1)
struct rbtreeKeyStct
{
    union
    {
        int32_t key32;
        int64_t key64;
        char keystr[RB_TREE_KEY_STR_MAX_SIZE];
    };
    uint8_t keytype;
};

struct rbtreeMgrStct
{
    struct rb_root rbroot;
    pthread_mutex_t rbtreelock;
    uint8_t keytype;
};
struct rbtreeNodeStct
{
    struct rb_node node;
    struct rbtreeKeyStct keyval;
    uint32_t valsize;
    uint32_t valtype;
    char usrval[1];
};
// #pragma pack(0)
typedef void (*rbTraversalCallback)(struct rbtreeNodeStct *rbUsrNode);

int rbtreeMgrFreeNode(struct rbtreeMgrStct *rbmgr, struct rbtreeNodeStct *eraseNode);
struct rbtreeNodeStct *rbtreeMgrFindNode(struct rbtreeMgrStct *rbmgr, const struct rbtreeKeyStct *findkey);
int rbtreeMgrInsertNode(struct rbtreeMgrStct *rbmgr, struct rbtreeNodeStct *inode);
// 替换数据
int rbtreeMgrReplaceNode(struct rbtreeMgrStct *rbmgr, struct rbtreeNodeStct *inode);
int rbtreeMgrEraseByKey(struct rbtreeMgrStct *rbmgr, const struct rbtreeKeyStct *findkey);
struct rbtreeNodeStct *rbtreeGetLRLimitNode(struct rbtreeMgrStct *rbmgr, int leftLimitFlag);
// 初始化管理器
int rbtreeMgrInit(struct rbtreeMgrStct *rbmgr, uint8_t setKeyTyep);
// 清空管理器
int rbtreeMgrFreeAll(struct rbtreeMgrStct *rbmgr);
// 遍历二叉树。
// traversalflag,0:前序，1：中序，2：后序
void rbtreeMgrTraversal(struct rbtreeMgrStct *rbmgr, rbTraversalCallback cbkFunc, int traversalflag);

#define rbtreeNodeHeadSize (offsetof(struct rbtreeNodeStct, usrval))
struct rbtreeNodeStct *rbtreeMallocOneNode(uint8_t setKeyType, uint32_t setValSize);
#define rbtreeGetValPtr(rbUsrNode, usrValType) ((usrValType *)rbUsrNode->usrval)

